package com.wmh.baseservice.operationaid.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wmh.baseservice.common.mybatisplus.structure.LambdaQueryWrapperX;
import com.wmh.baseservice.operationaid.entity.view.PddStoreDataView;
import com.wmh.baseservice.operationaid.excel.PddStoreExcel;
import com.wmh.baseservice.operationaid.mapper.PddStoreDataViewMapper;
import com.wmh.baseservice.operationaid.pojo.vo.store.PddStoreListReqVO;
import com.wmh.baseservice.operationaid.service.PddStoreDataViewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 牛威龙
 * @description 针对表【pdd_store_data_view】的视图操作Service实现
 * @createDate 2023-09-13 12:38:51
 */
@Service
@Slf4j
public class PddStoreDataViewServiceImpl extends ServiceImpl<PddStoreDataViewMapper, PddStoreDataView>
        implements PddStoreDataViewService {

    @Resource
    private PddStoreDataViewMapper pddStoreDataViewMapper;

    @Override
    public List<PddStoreDataView> getPddStoreDataViewList(PddStoreListReqVO reqVO) {
        LambdaQueryWrapperX<PddStoreDataView> wrapper = new LambdaQueryWrapperX<PddStoreDataView>()
                .eq(StrUtil.isNotBlank(reqVO.getStoreName()), PddStoreDataView::getStoreName, reqVO.getStoreName());
        if (StrUtil.isAllNotBlank(reqVO.getStartDate(), reqVO.getEndDate())) {
            wrapper.between(PddStoreDataView::getDataDate,
                    LocalDateTimeUtil.parse(reqVO.getStartDate() + "T00:00:00"),
                    LocalDateTimeUtil.parse(reqVO.getEndDate() + "T00:00:00"));
        }

        return pddStoreDataViewMapper.selectList(wrapper);
    }

    /**
     * 拼多多店铺数据下载为 Excel 表格文件
     */
    @Override
    public void writeExcel(HttpServletResponse response, PddStoreListReqVO reqVO) {
        List<PddStoreDataView> pddStoreDataViewList = getPddStoreDataViewList(reqVO);

        List<PddStoreExcel> data = pddStoreDataViewList.stream()
                .map(pv -> {
                    PddStoreExcel pe = new PddStoreExcel();

                    BeanUtils.copyProperties(pv, pe);

                    return pe;
                })
                .collect(Collectors.toList());

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        String fileName ;
        try {
            fileName = URLEncoder.encode("fileName", "UTF-8");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }

        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        try {
            EasyExcel.write(response.getOutputStream(), PddStoreExcel.class).sheet("Sheet1").doWrite(data);
        } catch (IOException e) {
            log.error("下载拼多多店铺数据异常: {}", e.getMessage());
            throw new RuntimeException(e);
        }
    }
}
